Spring Annotation
✒️ 2025-06-24 01:46 내용 수정
Annotation 목록
- 참고 자료 : Jordan Simpson Spring Component Annotation, Spring ResponseBody annotation, Spring Component and Further Stereotype Annotation
- 일부 Annotation은 Spring에서만 사용하거나 Spring Boot에서만 사용한다.
- 클래스 패스(Classpath) : Java가 클래스 파일이나 리소스 파일을 찾는 경로다.
| Annotation | 설명 |
|---|---|
@SpringBootApplication |
Spring Boot 앱의 main class 에 사용@Configuration + @EnableAutoConfiguration + @ComponentScan |
@Configuration |
설정 클래스를 만들고 Bean을 등록함 |
@ComponentScan |
@Component 및 stereotype(@Service, @Reository, @Controller) Annotion이 부여된 Class들을 자동으로 Scan하여 Bean으로 등록 |
@EnableAutoConfiguration |
자동 설정 활성화 |
@EnableWebMvc |
Annotation 기반의 SpringMvc를 구성할 때 필요한 Bean 설정을 자동으로 설정함 |
@Bean |
RootContext에 등록할 Bean을 설정 |
@Component |
Spring에서 custom bean을 자동으로 감지하도록 설정 |
@Controller |
Component에서 세분화된 Annotation Controller로 사용할 클래스에 명시 |
@Service |
Component에서 세분화된 Annotation Service로 사용할 클래스에 명시 |
@Repository |
Component에서 세분화된 Annotation DAO에 주로 사용 |
@EnableAutoConfiguration |
클래스패스 기반으로 Bean 설정을 자동 등록 |
@Autowired |
애플리케이션 컨텍스트 내에서 자동 의존성 주입(DI) 시 사용 |
@Qualifier |
주입할 Bean이 여러 개일 때, 명시적으로 어느 Bean을 사용할지 지정 |
@Primary |
동일 타입의 Bean이 여러 개일 때, 기본(default) Bean으로 설정 |
@RestController |
@Controller + @ResponseBody 조합, REST API Controller 표시 |
@GetMapping, @PostMapping,@PutMapping, @DeleteMapping |
HTTP 메서드에 따라 REST endpoint를 선언 |
@RequestBody |
요청 Body(JSON/XML 등)을 Java 객체로 바인딩 |
@ResponseBody |
응답의 Body 포함 시 사용 |
@PathVariable |
URL 경로 변수(path variable)를 메서드 인자로 바인딩 |
@ModelAttribute |
요청 파라미터나 Form 데이터를 객체로 Java 객체로 바인딩 |
@ResponseStatus |
메서드 또는 예외 클래스에 HTTP 응답 코드 설정을 할 때 사용 |
@ExceptionHandler |
컨트롤러 내부에서 특정 예외를 처리하여 에러 응답 처리 |
@Transactional |
메서드 또는 클래스에 Transaction 경계 선언 |
@Entity, @Table, @Id 등 |
JPA 사용 시 Entity 클래스를 정의할 때 사용 |
@ConfigurationProperties |
외부 설정(application.yml/properties)을 객체에 바인딩할 때 사용 |
@EnableScheduling, @Scheduled |
스케줄링 기능 활성화 및 주기 실행 메서드 정의에 사용 |
@Cacheable, @CachePut, @CacheEvict |
메서드 결과를 캐싱하거나 갱신, 삭제할 때 사용 |
@RestControllerAdvice / @ControllerAdvice |
전역 예외 처리, 바인딩 설정, Model 설정 등 공통 컨트롤러 기능을 모아두는 클래스 |
@Validated / @Valid |
Spring에서 메서드나 DTO에 유효성 검사(b단언(regex, 범위 등)) 할 때 사용 |